// 多语言支持功能

// 文本映射对象，包含中英文翻译
const translations = {
    'zh-CN': {
        'nav.home': '首页',
        'nav.about': '关于我',
        'nav.portfolio': '作品展示',
        'nav.contact': '联系方式',
        'hero.title': '欢迎访问我的作品集',
        'hero.subtitle': '我是一名充满激情的开发者，专注于创建优秀的数字体验',
        'hero.cta': '了解更多',
        'about.title': '关于我',
        'about.position': 'Python Developer',
        'about.description1': '我是一名经验丰富的Python开发者，擅长消息中间件、实时数据处理和高可用架构设计。',
        'about.description2': '在技术领域，我热衷于探索新技术和解决方案，不断挑战自我。同时，我也是一名Rust爱好者，喜欢研究系统级编程。',
        'about.description3': '在工作之余，我喜欢观看动画，特别是幸运星、孤独摇滚和摇曳露营，这些作品给了我很多灵感和放松的时刻。',
        'about.skills': '技能专长',
        'about.experience': '专业经历',
        'about.achievements': '个人成就',
        'experience.2010_2014.institution': '清华大学',
        'experience.2010_2014.degree': '本科，计算机科学与技术',
        'experience.2014_2016.institution': '斯坦福大学',
        'experience.2014_2016.degree': '硕士，计算机科学',
        'experience.2017_2020.company': 'Google',
        'experience.2017_2020.position': '软件工程师',
        'experience.2021_2023.company': 'OpenAI',
        'experience.2021_2023.position': '软件工程师',
        'achievement.1.title': '清华大学优秀毕业生',
        'achievement.2.title': '微软MVP',
        'achievement.3.title': '华为开发者认证',
        'portfolio.title': '作品展示',
        'portfolio.item1.title': '项目名称 1',
        'portfolio.item1.desc': '项目描述和主要功能特点',
        'portfolio.item1.link': '查看详情',
        'portfolio.item2.title': '项目名称 2',
        'portfolio.item2.desc': '项目描述和主要功能特点',
        'portfolio.item2.link': '查看详情',
        'portfolio.item3.title': '项目名称 3',
        'portfolio.item3.desc': '项目描述和主要功能特点',
        'portfolio.item3.link': '查看详情',
        'contact.title': '联系方式',
        'contact.info': '联系信息',
        'contact.form': '发送消息',
        'contact.name': '您的姓名',
        'contact.email': '您的邮箱',
        'contact.message': '您的留言',
        'contact.submit': '发送消息',
        'footer.copyright': '个人作品集. 保留所有权利.',
        'form.success': '消息发送成功！我会尽快回复您。',
        'form.error.required': '请填写所有必填字段',
        'form.error.email': '请输入有效的邮箱地址',
        'notification.view-project': '查看项目: ',
        'notification.redirect': '即将跳转到'
    },
    'en': {
        'nav.home': 'Home',
        'nav.about': 'About',
        'nav.portfolio': 'Portfolio',
        'nav.contact': 'Contact',
        'hero.title': 'Welcome to My Portfolio',
        'hero.subtitle': 'I am a passionate developer focused on creating exceptional digital experiences',
        'hero.cta': 'Learn More',
        'about.title': 'About Me',
        'about.position': 'Python Developer',
        'about.description1': 'I am an experienced Python developer skilled in message middleware, real-time data processing, and high-availability architecture design.',
        'about.description2': 'In the technical field, I am enthusiastic about exploring new technologies and solutions, constantly challenging myself. I am also a Rust enthusiast who enjoys studying system-level programming.',
        'about.description3': 'In my free time, I enjoy watching anime, particularly Lucky Star, Bocchi the Rock!, and Laid-Back Camp, which provide me with inspiration and relaxation.',
        'about.skills': 'Skills',
        'about.experience': 'Professional Experience',
        'about.achievements': 'Achievements',
        'experience.2010_2014.institution': 'Tsinghua University',
        'experience.2010_2014.degree': 'Bachelor\'s Degree, Computer Science and Technology',
        'experience.2014_2016.institution': 'Stanford University',
        'experience.2014_2016.degree': 'Master\'s Degree, Computer Science',
        'experience.2017_2020.company': 'Google',
        'experience.2017_2020.position': 'Software Engineer',
        'experience.2021_2023.company': 'OpenAI',
        'experience.2021_2023.position': 'Software Engineer',
        'achievement.1.title': 'Outstanding Graduate of Tsinghua University',
        'achievement.2.title': 'Microsoft MVP',
        'achievement.3.title': 'Huawei Developer Certification',
        'portfolio.title': 'Portfolio',
        'portfolio.item1.title': 'Project Name 1',
        'portfolio.item1.desc': 'Project description and main features',
        'portfolio.item1.link': 'View Details',
        'portfolio.item2.title': 'Project Name 2',
        'portfolio.item2.desc': 'Project description and main features',
        'portfolio.item2.link': 'View Details',
        'portfolio.item3.title': 'Project Name 3',
        'portfolio.item3.desc': 'Project description and main features',
        'portfolio.item3.link': 'View Details',
        'contact.title': 'Contact',
        'contact.info': 'Contact Information',
        'contact.form': 'Send Message',
        'contact.name': 'Your Name',
        'contact.email': 'Your Email',
        'contact.message': 'Your Message',
        'contact.submit': 'Send Message',
        'footer.copyright': 'Personal Portfolio. All rights reserved.',
        'form.success': 'Message sent successfully! I will reply to you as soon as possible.',
        'form.error.required': 'Please fill in all required fields',
        'form.error.email': 'Please enter a valid email address',
        'notification.view-project': 'View project: ',
        'notification.redirect': 'Redirecting to '
    }
};

// 当前语言
let currentLanguage = 'zh-CN';

// 切换语言函数
export function changeLanguage(language) {
    if (!translations[language]) {
        console.error('Unsupported language:', language);
        return;
    }
    
    currentLanguage = language;
    
    // 更新HTML元素的文本内容
    document.querySelectorAll('[data-i18n]').forEach(element => {
        const key = element.getAttribute('data-i18n');
        if (translations[language][key]) {
            element.textContent = translations[language][key];
        }
    });
    
    // 更新placeholder
    document.querySelectorAll('[data-i18n-placeholder]').forEach(element => {
        const key = element.getAttribute('data-i18n-placeholder');
        if (translations[language][key]) {
            element.setAttribute('placeholder', translations[language][key]);
        }
    });
    
    // 更新页面标题
    document.title = currentLanguage === 'zh-CN' ? '个人作品集' : 'Personal Portfolio';
    
    // 保存语言偏好到localStorage
    localStorage.setItem('preferredLanguage', language);
}

// 初始化语言设置
export function initLanguage() {
    // 从localStorage获取保存的语言偏好，如果没有则使用浏览器默认语言
    const savedLanguage = localStorage.getItem('preferredLanguage');
    const browserLanguage = navigator.language || navigator.userLanguage;
    
    const initialLanguage = savedLanguage || (browserLanguage.startsWith('zh') ? 'zh-CN' : 'en');
    changeLanguage(initialLanguage);
    
    // 设置语言切换按钮的状态
    document.querySelectorAll('.language-selector button').forEach(button => {
        button.classList.remove('active');
        if (button.dataset.lang === initialLanguage) {
            button.classList.add('active');
        }
    });
}

// 获取当前语言文本的工具函数
export function getCurrentText(key) {
    return translations[currentLanguage][key] || key;
}